//
//  UserHitCustomView.swift
//  BWUIKit
//
//  Created by zhuxuhong on 2022/3/22.
//

import UIKit

public final class UserHitsCustomView: UIView {
    @IBOutlet public var hitEnabledViews: [UIView] = []
    @IBOutlet public var hitDisabledViews: [UIView] = []

    override public func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        var result = super.hitTest(point, with: event)
        
        if result == nil, let _ = hitEnabledViews.first(where: {
            let newPoint = $0.convert(point, from: self)
            return $0.point(inside: newPoint, with: event)
        }) {
            isUserInteractionEnabled = true
            result = super.hitTest(point, with: event)
            isUserInteractionEnabled = false
        }
        
        if result != nil, let _ = hitDisabledViews.first(where: {
            let newPoint = $0.convert(point, from: self)
            return $0.point(inside: newPoint, with: event)
        }) {
            result = nil
        }
        
        return result
    }
}
